package org.rcgwt.client.ui.calendar.model.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.rcgwt.client.theme.CssStyle;
import org.rcgwt.client.ui.calendar.model.IEventStyleMapper;
import org.rcgwt.client.ui.themes.ITheme;

public class EventStyleMapper implements IEventStyleMapper {

	private ITheme theme;
	private Map<EventType, CssStyle> eventsStyle;
	private Map<EventType, Boolean> selectables;
	private Map<EventType, Boolean> navigables;

	public EventStyleMapper(ITheme theme) {
		this.theme = theme;

		eventsStyle = new HashMap<EventType, CssStyle>();
		selectables = new HashMap<EventType, Boolean>();
		navigables = new HashMap<EventType, Boolean>();

		fillMaps();
	}

	private void fillMaps() {
		/* Events Styles */
		addEventStyle(EventType.BUSY, theme.getBusyDayStyle());
		addEventStyle(EventType.HOLIDAYS, theme.getHolidaysDayStyle());
		addEventStyle(EventType.OTHERS, theme.getOthersDayStyle());
		addEventStyle(EventType.SPECIAL, theme.getSpecialDayStyle());
		addEventStyle(EventType.G_SYNCHRONIZED, theme.getBusyDayStyle());

		/* Navigable attributes */
		addNavigableStyle(EventType.BUSY, false);
		addNavigableStyle(EventType.HOLIDAYS, false);
		addNavigableStyle(EventType.OTHERS, true);
		addNavigableStyle(EventType.SPECIAL, true);
		addNavigableStyle(EventType.G_SYNCHRONIZED, false);

		/* Selectable attributes */
		addSelectableStyle(EventType.BUSY, false);
		addSelectableStyle(EventType.HOLIDAYS, false);
		addSelectableStyle(EventType.OTHERS, false);
		addSelectableStyle(EventType.SPECIAL, true);
		addSelectableStyle(EventType.G_SYNCHRONIZED, false);

	}

	/* Overriding Method */

	@Override
	public void addEventStyle(EventType type, CssStyle style) {
		eventsStyle.put(type, style);
	}

	@Override
	public void addNavigableStyle(EventType type, boolean navigable) {
		navigables.put(type, navigable);
	}

	@Override
	public void addSelectableStyle(EventType type, boolean selectable) {
		selectables.put(type, selectable);
	}

	/* Reciprocal mapping */

	public CssStyle getStyle(EventType eventType) {
		return eventsStyle.get(eventType);
	}

	public EventType getEventType(String style) {
		Set<Entry<EventType, CssStyle>> set = eventsStyle.entrySet();
		for (Entry<EventType, CssStyle> e : set) {
			if (e.getValue() == null) {
				continue;
			}
			if (style.contains(e.getValue().getClassName()) == true) {
				return e.getKey();
			}
		}
		return null;
	}

	/* Selectable/Navigable attributes */

	public boolean isSelectable(String style) {
		return isSelectable(getEventType(style));
	}

	public boolean isSelectable(EventType type) {
		Boolean b = selectables.get(type);
		if (b == null) {
			return true;
		}
		return b;
	}

	public boolean isNavigable(String style) {
		return isNavigable(getEventType(style));
	}

	public boolean isNavigable(EventType type) {
		Boolean b = navigables.get(type);
		if (b == null) {
			return true;
		}
		return b;
	}

}
